﻿using System;
using System.ComponentModel.Composition;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Unity;
using Silverlight.Weblog.Client.CoreBL.Events;
using Silverlight.Weblog.Client.DAL.Events;
using Silverlight.Weblog.Common.IoC;
using Silverlight.Weblog.Server.DAL;

namespace Silverlight.Weblog.Client.DAL.Services
{
    [Export(typeof(IInitializationService))]
    public class NavigationFragmentService : IInitializationService 
    {
        public void StartService(ApplicationServiceContext context)
        {
            
        }

        public void Initialize()
        {
            IEventAggregator events = IoC.Get<IEventAggregator>();
            events.GetEvent<NavigateToBlogPostEvent>().Subscribe(NavigateToBlogPost);
            events.GetEvent<GoToBlogPostPermlinkEvent>().Subscribe(NavigateToBlogPostPermlinkEvent);
            events.GetEvent<NavigateToHomePageEvent>().Subscribe(NavigateToHomePage);
            events.GetEvent<DisplayCategoryEvent>().Subscribe(NavigateToCategory);
            events.GetEvent<SearchTermEvent>().Subscribe(NavigateToSearch);
        }


        public void NavigateToSearch(SearchEventData obj)
        {
            MoveToFragment("Search", obj.SearchTerm, "Username", obj.Username);   
        }

        public void NavigateToCategory(Category obj)
        {
            MoveToFragment("Category", obj.Name, "Username", obj.User.Username);
        }

        public void NavigateToBlogPostPermlinkEvent(string obj)
        {
            MoveToFragment("BlogPost", obj);
        }

        public void NavigateToHomePage(User obj)
        {
            MoveToFragment("Home", obj.Username);
        }

        public void NavigateToBlogPost(BlogPost obj)
        {
            MoveToFragment("BlogPost", obj.Permlink);
        }

        private void MoveToFragment(string key, string value)
        {
            Application.Current.Host.NavigationState = string.Format("{0}={1}", key, value);
        }

        private void MoveToFragment(string key, string value, string key2, string value2)
        {
            try
            {
                Application.Current.Host.NavigationState = string.Format("{0}={1}&{2}={3}", key, value, key2, value2);
            }
            catch
            {
#if DEBUG
                throw;
#endif
            }
        }


        public void StopService()
        {
            IEventAggregator events = IoC.Get<IEventAggregator>();
            events.GetEvent<NavigateToBlogPostEvent>().Unsubscribe(NavigateToBlogPost);
            events.GetEvent<GoToBlogPostPermlinkEvent>().Unsubscribe(NavigateToBlogPostPermlinkEvent);
            events.GetEvent<NavigateToHomePageEvent>().Unsubscribe(NavigateToHomePage);
        }



    }
}
